package com.yisoo.ubag.iot;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.iot.model.v20180120.PubRequest;
import com.aliyuncs.iot.model.v20180120.PubResponse;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.commons.codec.binary.Base64;

public class PopPubServer {
//    sub topic:
//            +QMTRECV: 0,0,"/a19pJ9fEVwE/1ec8_bag/user/sub/order","767"
//
//    sub topic:
//            +QMTRECV: 0,0,"/a19pJ9fEVwE/1ec8_bag/user/sub/resp","{"accuracy":0.001,"time":now}"
//
//    sub topic:
//            +QMTRECV: 0,0,"/a19pJ9fEVwE/1ec8_bag/user/sub/resp","{"accuracy":0.001,"time":now}"
//
//    sub topic:
//            +QMTRECV: 0,0,"/a19pJ9fEVwE/1ec8_bag/user/sub/resp","{"accuracy":0.001,"time":now}"
    public static void main(String[] args) {
        String regionId = "cn-shanghai";
        String accessKey = "LTAI4GJsxqB4enkdqv5VievY";
        String accessSecret = "NVhn0K3qBRsiC2KuQDnQtlnxI4Lacv";
        final String productKey = "a19pJ9fEVwE";
        final String deviceName = "1ec8_bag";
        //设置client的参数
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKey, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        PubRequest request = new PubRequest();
        request.setQos(0);
        //设置发布消息的topic
        request.setTopicFullName("/" + productKey + "/" + deviceName + "/user/sub/resp");
        request.setProductKey(productKey);
        //设置消息的内容，一定要用base64编码，否则乱码
        request.setMessageContent(Base64.encodeBase64String("{}".getBytes()));
        int  n  = 3;
        while(n!=0){
            n--;
            try {
                PubResponse response = client.getAcsResponse(request);
                System.out.println("pub success?:" + response.getSuccess());
                Thread.sleep(3000);
            } catch (Exception e) {
                System.out.println(e);
            }

        }

    }
}
